package china.accountinghelper.adapter;

import com.xdroid.base.AdapterBase;
import china.wc.tallyhelper.R;
import china.wc.tallyhelper.business.BusinessPerson;
import china.wc.tallyhelper.model.ModelPerson;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 本类是人员管理页面中，PersonList用的Adapter。
 * 
 * @author 王超
 * 
 */
public class AdapterListViewPersonList extends AdapterBase<ModelPerson>
{
    public static final int SELECT_ALL     = 0;
    public static final int SELECT_ENABLED = 1;

    private BusinessPerson  mBusinessPerson;
    private int             mModel;

    public AdapterListViewPersonList(Context pContext, int pModel)
    {
        super(pContext, null);
        mBusinessPerson = new BusinessPerson();
        mModel = pModel;

        if (mModel == SELECT_ALL) {
            setList(mBusinessPerson.select());
        } else if (mModel == SELECT_ENABLED) {
            setList(mBusinessPerson.selectEnabledPerson());
        }
    }
    public AdapterListViewPersonList(Context pContext)
    {
        this(pContext, SELECT_ALL);
    }
    
    @Override
    public long getItemId(int pPosition)
    {
        return getList().get(pPosition).getID();
    }

    // 返回绑定好PersonName的View
    @Override
    public View getView(int pPosition, View pConvertView, ViewGroup pParent)
    {

        if (pConvertView == null) {
            pConvertView = this.getLayoutInflater().inflate(R.layout.item_listview_personlist, null);
        }

        ModelPerson _ModelPersonObject = getList().get(pPosition);

        TextView _TextView = (TextView) pConvertView.findViewById(R.id.ItemListViewPersonList_TextView_Name);
        _TextView.setText(_ModelPersonObject.getName());

        if (_ModelPersonObject.getState() == ModelPerson.STATE_DISABLED) {
            _TextView.setTextAppearance(getContext(), R.style.TextView_ListViewItem_Normal_Red);
        } else if (_ModelPersonObject.getState() == ModelPerson.STATE_ENABLED) {
            _TextView.setTextAppearance(getContext(), R.style.TextView_ListViewItem_Normal);
        } else if (_ModelPersonObject.getState() == ModelPerson.STATE_INDISPENSABLE) {
            _TextView.setTextAppearance(getContext(), R.style.TextView_ListViewItem_Normal);
        }

        return pConvertView;
    }

    /**
     * 刷新数据源。
     */
    @Override
    public void notifyDataSetChanged()
    {
        if (mModel == SELECT_ALL) {
            setList(mBusinessPerson.select());
        } else if (mModel == SELECT_ENABLED) {
            setList(mBusinessPerson.selectEnabledPerson());
        }
        super.notifyDataSetChanged();
    }

}
